package com.bangyu.monitor;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: zhangxue
 * @Date: 19/8/25 21:34
 * @Description:
 */
@Slf4j
public class Test {

    public static void main(String[] args) throws IOException {
        String rootPath = "/Users/zhangxue/IdeaProjects/monitor/";
        String binaryPath = rootPath+File.separator+"binary";
        String deviceNo = "0104711906171";
        File file = new File(binaryPath+File.separator+deviceNo+".bin");
        File file1 = new File(binaryPath+File.separator+deviceNo+".csv");

        InputStream is = new FileInputStream(file);
        OutputStream os = new FileOutputStream(file1,true);
        int offse = 0;
        int abailable = 0;
        try{
            while ((abailable = is.available())>0){
                byte [] datas = new byte[322];
                is.read(datas,0,322);
                offse +=322;
                if(datas.length==322){
                    byte[] tdBytes = Arrays.copyOfRange(datas,0,320);//通道数据
                    byte[] temperatureBytes = Arrays.copyOfRange(datas,320,322);//温度
                    Integer temperatureInt = bytes2Int(temperatureBytes);

                    //将二进制数据转换成160组位移数据
                    List<Integer> floats = Lists.newArrayList();
                    for(int i=0;i<160;i++){
                        int from = i*2;
                        int to = (i+1)*2;
                        System.out.println(from+"-"+to);
                        byte[] b = Arrays.copyOfRange(tdBytes,from,to);
                        int ivalue1 = bytes2Int(b);
                        floats.add(ivalue1);
                    }
                    List<List> twentyGroups = Lists.newArrayList();
                    for(int j=0;j<10;j++){
                        List<Integer> list = floats.subList(j*16,(j+1)*16);
                        List list1 = Lists.newArrayList();
                        List list2 = Lists.newArrayList();
                        for(int k=0;k<list.size();k++){
                            if(k%2==0){
                                list1.add(list.get(k));
                            }else{
                                list2.add(list.get(k));
                            }
                        }
                        twentyGroups.add(list1);
                        twentyGroups.add(list2);
                    }
                    for(List list:twentyGroups){
                        StringBuilder sb = new StringBuilder();
                        for(int i=0;i<list.size();i++){
                            sb.append(list.get(i));
                            if(i!=list.size()-1){
                                sb.append(",");
                            }else{
                                sb.append("\r");
                            }
                        }
                        os.write(sb.toString().getBytes());
                        os.flush();
                    }


                }
            }
        }catch (Exception e){
            log.info(offse+"",e);
        }
        finally {
            os.close();
        }


    }

    private static Integer bytes2Int(byte[] bytes){
        return ((bytes[0] & 0xFF)
                | (bytes[1] & 0xFF) << 8);
    }
}
